﻿using System;
using com.yoozoo.gta.Gameplay.RTS;
using UnityEngine;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gameplay.RTS.Proto;

public class MarchHudLOD1Point : MonoBehaviour
{
    /// <summary>
    /// 绿点
    /// </summary>
    public SpriteRenderer TeamColorIcon;

    public SpriteRenderer SoldierTypeIcon;
    private int team = -1;
    private string[] teamColorIconName = new string[]
    {
        "img_map_kind02",
        "img_map_kind03",
        "img_map_kind04",
        "img_map_kind01"
    };

    private bool visible = true;
    
    public long Tid = 0L;
    public void SetTeamIconVisible(bool visible)
    {
        if (this.visible != visible)
        {
            TeamColorIcon.enabled = visible;
            SoldierTypeIcon.enabled = visible;
            this.visible = visible;
        }
    }

    public void SetSoldierTypeIconVisible(bool vis)
    {
        SoldierTypeIcon.enabled = vis;
    }
    internal void SetTeam(int team,long tid=0)
    {
        if (team != this.team)
        {
            ArmyManager.Instance.GetRtsResourceManager().SetTeamColorIcon(teamColorIconName[team], TeamColorIcon);
            this.team = team;
        }
        Tid = tid;
    }

    internal void SetSoldierTypeIcon(RtsServerInfo serverData,EClientMarchType clientMarchType)
    {
        string type;
        if (clientMarchType == EClientMarchType.Scout)
        {
            type = "icon_setoff_spy";
        }
        else
        {
            if (NeedShowMixedSoldierIcon(serverData))
            {
                type = "icon_setoff_mixedstable";
            }
            else
            {
                switch (serverData.SoldierType)
                {
                    case 1:
                        type = "icon_setoff_barracks";
                        break;
                    case 2:
                        type = "icon_setoff_stable";
                        break;
                    case 3:
                        type = "icon_setoff_range";
                        break;
                    case 4:
                        type = "icon_setoff_siege";
                        break;
                    default:
                        type = "img_map_reinforce";
                        break;
                }
            }
        }
       
        ArmyManager.Instance.ResourceManager.SetHeroTypeIcon(type, SoldierTypeIcon);
    }

    public void SetTruckIcon(int truckType)
    {
        var icon = "icon_map_IntercityTrade_" + truckType;
        ArmyManager.Instance.ResourceManager.SetMapElementAtlasIcon(icon, SoldierTypeIcon);
    }

    public void SetTrianIcon(int team)
    {
        var icon = "icon_map_train_blue";
        if (team == (int)MarchTeam.Enemy)
        {
            icon = "icon_map_train_red";
        }
        ArmyManager.Instance.ResourceManager.SetMapElementAtlasIcon(icon, SoldierTypeIcon);
    }
    
    public void SetIconFlipped(bool isFlipped)
    {
        SoldierTypeIcon.flipX = isFlipped;
    }
    
    private bool NeedShowMixedSoldierIcon(RtsServerInfo serverData)
    {
        //有集结部队,且在集结状态需要显示混兵
        if ((serverData.marchType == MarchType.ASSEMBLY_JOIN_11 || serverData.marchType == MarchType.ASSEMBLY_OUT_12) &&
            serverData.assemblyId > 0 && serverData.isAssemblyLeader && serverData.AssemblyIconType == 0)
        {
                
            return true;
        }
            
        return false;
    }
}
